<template>
  <div class="rule-index">
    <div class="toolbar">
      <el-button icon="el-icon-refresh" @click="refresh" :loading="loading"></el-button>
      <el-button type="danger" @click="add">添加</el-button>
    </div>

    <el-table border v-loading="loading" :data="data" row-key="id" :default-expand-all="true">
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="描述" prop="description"></el-table-column>
      <el-table-column label="请求方式" prop="method"></el-table-column>
      <el-table-column label="模块" prop="module"></el-table-column>
      <el-table-column label="控制器" prop="controller"></el-table-column>
      <el-table-column label="方法" prop="action"></el-table-column>
      <el-table-column label="路由" prop="router"></el-table-column>
      <el-table-column label="是否菜单" prop="is_menu">
        <template slot-scope="scope">
          <el-tag>{{scope.row.is_menu | menuStatus}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag>{{scope.row.status | userStatus}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图标" prop="icon"></el-table-column>
      <el-table-column label="操作" fixed="right" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      width="600px"
      top="5vh"
      :title="ruleForm.id ? '修改规则' : '添加规则'"
      :visible.sync="showDialog"
    >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="父级规则" prop="pid">
          <el-cascader
            :options="options"
            change-on-select
            :show-all-levels="false"
            v-model="ruleForm.pid"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="名称" prop="title">
          <el-input v-model="ruleForm.title" placeholder="请输入规则名称"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="ruleForm.description" type="textarea" placeholder="请输入规则描述"></el-input>
        </el-form-item>
        <el-form-item label="模块" prop="module">
          <el-input v-model="ruleForm.module" placeholder="admin"></el-input>
        </el-form-item>
        <el-form-item label="控制器" prop="controller">
          <el-input v-model="ruleForm.controller" placeholder="rule"></el-input>
        </el-form-item>
        <el-form-item label="方法" prop="action">
          <el-input v-model="ruleForm.action" placeholder="index"></el-input>
        </el-form-item>
        <el-form-item label="路由" prop="router">
          <el-input v-model="ruleForm.router" placeholder="/rule/:id$"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="ruleForm.icon" placeholder="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item label="请求方式" prop="method">
          <el-radio-group v-model="ruleForm.method">
            <el-radio label="get">GET</el-radio>
            <el-radio label="post">POST</el-radio>
            <el-radio label="delete">DELETE</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否菜单" prop="is_menu">
          <el-switch v-model="ruleForm.is_menu" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="是否显示" prop="is_show">
          <el-switch v-model="ruleForm.is_show" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-switch v-model="ruleForm.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="submit('ruleForm')" :loading="loading">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import table from '../../../mixins/table'

export default {
  name: 'ruleIndex',
  mixins: [table],
  data () {
    return {
      url: '/admin/rules',
      delUrl: '/admin/rule',
      addUrl: '/admin/rule',
      editUrl: '/admin/rule',
      options: { label: '顶级规则', value: 0 },
      rules: {
        pid: [{ required: true, message: '请选择父级规则', trigger: 'blur' }],
        title: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入规则描述', trigger: 'blur' }],
        module: [{ required: true, message: '请输入模块名称', trigger: 'blur' }],
        controller: [{ required: true, message: '请输入控制器名称', trigger: 'blur' }],
        action: [{ required: true, message: '请输入请求方法', trigger: 'blur' }],
        router: [{ required: true, message: '请输入路由', trigger: 'blur' }],
        method: [{ required: true, message: '请选择请求方式', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.loadData()

    this.loadRules()
  }
}
</script>
